package com.smugmugsoftware.airconcalc.advanced;

import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RoomComponentListAdapter<T> extends ArrayAdapter<T> {

	Context context;
	Vector<T> data;

	public RoomComponentListAdapter(Context context,Vector<T> data) {
		super(context, android.R.layout.simple_list_item_1, data);
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		RowHolder holder = null;

		if (data.get(position) instanceof Wall) {			

			if(row == null) {
				LayoutInflater inflater = ((Activity)context).getLayoutInflater();
				row = inflater.inflate(R.layout.wall_list_item, parent, false);

				holder = new RowHolder();
				holder.id = (TextView) row.findViewById(R.id.ID);
				holder.area = (TextView) row.findViewById(R.id.area);
				holder.constNo = (TextView) row.findViewById(R.id.constNo);
				holder.dimension = (TextView) row.findViewById(R.id.dimension);
				holder.facing = (TextView) row.findViewById(R.id.facing);

				row.setTag(holder);
			} else {
				holder = (RowHolder) row.getTag();
			}

			Wall thisComponent = (Wall) data.get(position);

			holder.dimension.setText(thisComponent.getLengthString() + " x " + thisComponent.getWidthString());
			holder.constNo.setText(thisComponent.getConstructionNumber());
			holder.area.setText(thisComponent.getAreaString());
			holder.facing.setText(thisComponent.getFacingString());
			holder.id.setText(String.valueOf(thisComponent.getID()));

		} else if (data.get(position) instanceof CeilingFloor) {

			if(row == null) {
				LayoutInflater inflater = ((Activity)context).getLayoutInflater();
				row = inflater.inflate(R.layout.cel_floor_list_item, parent, false);

				holder = new RowHolder();
				holder.id = (TextView) row.findViewById(R.id.ID);
				holder.idLabel = (TextView) row.findViewById(R.id.IDLabel);
				holder.area = (TextView) row.findViewById(R.id.area);
				holder.constNo = (TextView) row.findViewById(R.id.constNo);
				holder.dimension = (TextView) row.findViewById(R.id.dimension);
				holder.dimensionLabel = (TextView) row.findViewById(R.id.dimensionLabel);

				row.setTag(holder);
			} else {
				holder = (RowHolder) row.getTag();
			}

			CeilingFloor thisComponent = (CeilingFloor) data.get(position);

			holder.constNo.setText(thisComponent.getConstructionNumber());
			holder.area.setText(thisComponent.getAreaString());
			holder.id.setText(String.valueOf(thisComponent.getID()));
			holder.idLabel.setText(thisComponent.getTypeString());

			if (!thisComponent.usesArea()) {
				holder.dimension.setVisibility(View.VISIBLE);
				holder.dimensionLabel.setVisibility(View.VISIBLE);
				holder.dimensionLabel.setText("Dim:");
				holder.dimension.setText(thisComponent.getLengthString() + " x " + thisComponent.getWidthString());

			} else if (thisComponent.usesArea()) {

				if (thisComponent.isCathedral()) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Cathedral Ceiling");
					holder.dimension.setText("");

				} else if (thisComponent.isVaulted()) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Vaulted Ceiling");
					holder.dimension.setText("");
					
				} else {
					holder.dimensionLabel.setText("");
					holder.dimension.setVisibility(View.GONE);
					holder.dimensionLabel.setVisibility(View.GONE);
				}
			}
		} else if (data.get(position) instanceof WindowDoor) {

			if(row == null) {
				LayoutInflater inflater = ((Activity)context).getLayoutInflater();
				row = inflater.inflate(R.layout.win_door_list_item, parent, false);

				holder = new RowHolder();
				holder.id = (TextView) row.findViewById(R.id.ID);
				holder.idLabel = (TextView) row.findViewById(R.id.IDLabel);
				holder.area = (TextView) row.findViewById(R.id.area);
				holder.constNo = (TextView) row.findViewById(R.id.constNo);
				holder.facing = (TextView) row.findViewById(R.id.facing);
				holder.dimension = (TextView) row.findViewById(R.id.dimension);
				holder.dimensionLabel = (TextView) row.findViewById(R.id.dimensionLabel);
				holder.quantity = (TextView) row.findViewById(R.id.qty);
				holder.shade = (TextView) row.findViewById(R.id.shade_area);
				holder.shadePercent = (TextView) row.findViewById(R.id.shade_percent);

				row.setTag(holder);
			} else {
				holder = (RowHolder) row.getTag();
			}

			WindowDoor thisComponent = (WindowDoor) data.get(position);

			holder.constNo.setText(thisComponent.getConstructionNumber());
			holder.area.setText(String.valueOf((int) thisComponent.getAreaInFeet()));
			holder.id.setText(String.valueOf(thisComponent.getID()));
			holder.idLabel.setText(thisComponent.getTypeString());
			holder.quantity.setText(String.valueOf(thisComponent.getQty()));
			holder.facing.setText(thisComponent.getFacingString());
			holder.shade.setText(String.valueOf((int) thisComponent.getShadedAreaInFeet()));
			holder.shadePercent.setText(String.valueOf((int) ((thisComponent.getShadedArea() / thisComponent.getArea()) * 100)) + "%");

			if (!thisComponent.usesArea()) {
				holder.dimension.setVisibility(View.VISIBLE);
				holder.dimensionLabel.setVisibility(View.VISIBLE);
				holder.dimensionLabel.setText("Dim:");
				holder.dimension.setText(thisComponent.getLengthString() + " x " + thisComponent.getWidthString());

			} else if (thisComponent.usesArea()) {

				if (thisComponent.getType() == WindowDoor.WINDOW_ARCHED) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Arched Window");
					holder.dimension.setText("");

				} else if (thisComponent.getType() == WindowDoor.WINDOW_ELLIPSE) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Elliptical");
					holder.dimension.setText("");
					
				} else if (thisComponent.getType() == WindowDoor.WINDOW_HALF_ARCH) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Half Cathedral");
					holder.dimension.setText("");
					
				} else if (thisComponent.getType() == WindowDoor.WINDOW_HALF_ELLIPSE) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Chord");
					holder.dimension.setText("");
					
				} else if (thisComponent.getType() == WindowDoor.WINDOW_TRIANGULAR) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Peak/Triangular");
					holder.dimension.setText("");
					
				} else if (thisComponent.getType() == WindowDoor.WINDOW_SQUARE) {
					holder.dimension.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setVisibility(View.VISIBLE);
					holder.dimensionLabel.setText("Dim:");
					holder.dimension.setText(thisComponent.getLengthString() + " x " + thisComponent.getWidthString());
					
				} else if (thisComponent.getType() == WindowDoor.WINDOW_AREA) {
					holder.dimensionLabel.setText("");
					holder.dimension.setVisibility(View.GONE);
					holder.dimensionLabel.setVisibility(View.GONE);

				} else {
					holder.dimensionLabel.setText("");
					holder.dimension.setVisibility(View.GONE);
					holder.dimensionLabel.setVisibility(View.GONE);
				}
			}
		} else if (data.get(position) instanceof SensibleLoad) {

			if(row == null) {
				LayoutInflater inflater = ((Activity)context).getLayoutInflater();
				row = inflater.inflate(R.layout.sensible_load_list_item, parent, false);

				holder = new RowHolder();
				holder.id = (TextView) row.findViewById(R.id.ID);
				holder.idLabel = (TextView) row.findViewById(R.id.IDLabel);
				holder.constNo = (TextView) row.findViewById(R.id.constNo);
				holder.quantity = (TextView) row.findViewById(R.id.qty);				
				holder.load = (TextView) row.findViewById(R.id.load);

				row.setTag(holder);
			} else {
				holder = (RowHolder) row.getTag();
			}

			SensibleLoad thisComponent = (SensibleLoad) data.get(position);

			holder.id.setText(String.valueOf(thisComponent.getID()));
			holder.idLabel.setText(thisComponent.getTypeString());
			holder.constNo.setText(thisComponent.getConstructionNumber());
			holder.quantity.setText(String.valueOf(thisComponent.getQty()));
			holder.load.setText(String.valueOf(thisComponent.getTotalBtu()));
		}
		return row;
	}

	static class RowHolder
	{
		public TextView dimensionLabel;
		TextView area;
		TextView dimension;
		TextView facing;
		TextView constNo;
		TextView id;
		TextView idLabel;
		TextView shade;
		TextView quantity;
		TextView shadePercent;
		TextView load;
	}
}
